
clear
clear all
clear mata
clear matrix
set more off
set matsize 11000
set maxvar 30000
cap log off
capture log close
set emptycells drop
pause on


***********************************************
* USER DEFINE FILEPATH FOR REPLICATION FOLDER *
***********************************************

global path "C:\Users\wb520443\Dropbox\Research Projects\Global pollution\2_analysis\Replication"



***********************************************


global opts		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))  ) star(* .10 ** .05 *** .01) noobs nocons

global opts_fs		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))) star(* .10 ** .05 *** .01) noobs nocons


global input "$path\data"
global figdat "$path\figdat"
global figures "$path\figures"
global tables "$path\tables"



cap mat define countryfigure=J(105,10,.)
		local row=1
		
	use "$input/rwi_pollution_analysis_collapsed.dta", replace
	
	replace error=1/error
	g both=population*error
	g both2=country_population*error
	
	keep if country=="IND_PAK"|country=="IDN"|country=="BRA"|country=="NGA"|country=="BGD"|country=="MEX"|country=="ETH"|country=="PHL"|country=="VNM"|country=="COD"
	
	foreach var in IND_PAK IDN BRA NGA BGD MEX ETH PHL VNM COD{
		preserve
			keep if country=="`var'"
			
			*replace rwi=round(rwi,0.01)
				*replace rwi=2 if rwi>2
			
			*bysort rwi: egen wt_pm=wtmean(mean_pm25), weight(both)
			
			*lowess mean_pm25 rwi
		
			*duplicates drop rwi, force
			
			g rwi1=round(rwi,0.01)
			g rwi2=round(rwi,0.1)
			g rwi3=round(rwi,0)
			
			bysort rwi3: egen wt_pm3=wtmean(mean_pm25), weight(both)
			bysort rwi2: egen wt_pm2=wtmean(mean_pm25), weight(both)
			bysort rwi1: egen wt_pm1=wtmean(mean_pm25), weight(both)
			
			sort rwi3
				replace rwi3=. if rwi3==rwi3[_n-1]
			sort rwi2
				replace rwi2=. if rwi2==rwi2[_n-1]	
			sort rwi1
				replace rwi1=. if rwi1==rwi1[_n-1]
			
			sort rwi
			
			twoway (scatter wt_pm3 rwi3, mc(gs13)) (scatter wt_pm1 rwi1, mc(emerald%50)) (scatter wt_pm2 rwi2, mc(ebblue) ///
					xti("RWI",size(vlarge)) xlabel(,nogrid labsize(large)) ylabel(,nogrid labsize(large))  ytitle("Population & error weighted" "average pollution",size(vlarge)) xsize(5) ysize(2) legend(pos(6) col(3) label(1 "RWI, 0.000") label(2 "RWI, 0.00") label(3 "RWI, 0.0") size(vlarge)))
					
					graph export "$figures/kuznets_curve_`var'.png", as(png) replace
					
		restore			
		
	}
	
	*replace rwi=round(rwi,0.1)
	*			replace rwi=2 if rwi>2
			
	*bysort rwi: egen wt_pm=wtmean(mean_pm25), weight(both2)
			
	*duplicates drop rwi, force
	*	sort rwi
			
	g rwi1=round(rwi,0.01)
			g rwi2=round(rwi,0.1)
			g rwi3=round(rwi,0)
			
			bysort rwi3: egen wt_pm3=wtmean(mean_pm25), weight(both)
			bysort rwi2: egen wt_pm2=wtmean(mean_pm25), weight(both)
			bysort rwi1: egen wt_pm1=wtmean(mean_pm25), weight(both)
			
			sort rwi3
				replace rwi3=. if rwi3==rwi3[_n-1]
			sort rwi2
				replace rwi2=. if rwi2==rwi2[_n-1]	
			sort rwi1
				replace rwi1=. if rwi1==rwi1[_n-1]
			
			sort rwi
			
			twoway (scatter wt_pm3 rwi3, mc(gs13)) (scatter wt_pm1 rwi1, mc(emerald%50)) (scatter wt_pm2 rwi2, mc(ebblue) ///
					xti("RWI",size(large)) xlabel(,nogrid labsize(medlarge)) ylabel(,nogrid labsize(medlarge))  ytitle("Population & error weighted" "average pollution", size(large)) xsize(5) ysize(3) legend(pos(6) col(3) label(1 "RWI, 0.000") label(2 "RWI, 0.00") label(3 "RWI, 0.0") size(medlarge)))
					
		graph export "$figures/kuznets_curve_all.png", as(png) replace
		